﻿@model PagedResult<order_dto>
@{ string site_img = ViewBag.img_url;}
@{
    if (Model.rows.Count > 0) {
        foreach (var row in Model.rows) {
            <li>
                <div class="orderNumber fn28">
                    @if (row.status == OrderStatus.WaitPay) {
                        <span class="fn-right col-blue fn-bold">待支付</span>
                    } else if (row.status == OrderStatus.WaitDelivery) {
                        <span class="fn-right col-red fn-bold">待发货</span>
                    } else if (row.status == OrderStatus.Confirm) {
                        <span class="fn-right col-green fn-bold">待收货</span>
                    } else if (row.status == OrderStatus.Finish) {
                        <span class="fn-right col-gray">已完成</span>
                    } else if (row.status == OrderStatus.Close || row.status == OrderStatus.None) {
                        <span class="fn-right col-gray">已关闭</span>
                    }
                    <span>
                        <em class="col-gray2">订单号:</em>
                        @row.order_no
                    </span>
                </div>

                @foreach (var item in row.order_items) {
                    decimal itemPrice = item.unit_price * item.quantity;
                    decimal itemRedDrill = item.red_drill * item.quantity;
                    <a href="/MyOrder/OrderDetail?order_id=@row.id">
                        <div class="content">
                            <div class="img">
                                @if (item.order_item_type != 6) {
                                    <img src="@(item.product.main_image_url != null ? item.product.main_image_url : site_img + item.product.first_image)" />
                                } else {
                                    <img src="@(site_img +item.bazaar_product.main_img)"/>
                                }

                            </div>
                            <div class="text">
                                <p class="fn28 mb30">
                                    @if (item.order_item_type != 6) {@item.product.name} else {@item.bazaar_product.product_name}

                                </p>
                                <div class="col-gray fn24 mb30">
                                    数量：<em class="col-gray2"> @item.quantity 件</em>
                                </div>
                                <div class="price col-gray">
                                    @if (itemPrice > 0) {
                                        <span class="col-red fn30">@(itemPrice.ToString("0.##"))</span><span>元</span>
                                    }
                                    @if (itemPrice > 0 && itemRedDrill > 0) {
                                        <span>+</span>
                                    }
                                    @if (itemRedDrill > 0) {
                                        <span class="col-red fn30">@(itemRedDrill.ToString("0.######"))</span><span>红钻</span>

                                    }
                                </div>
                            </div>
                        </div>
                    </a>
                }
                <div class="btnBox">
                    @if (row.status == OrderStatus.WaitPay) {
                        <button class="ui-btn ui-btn-border cancel" type="button" value="@row.id">取消订单</button>
                        <button class="ui-btn ui-btn-border pay" type="button" value="@row.order_no">立即支付</button>
                    }
                    @if (row.status == OrderStatus.Finish || row.status == OrderStatus.Close) {
                        <button class="ui-btn ui-btn-border delete" type="button" value="@row.id">删除订单</button>
                    }
                    @if (row.status == OrderStatus.Confirm) {
                        <button class="ui-btn ui-btn-border receipt" type="button" value="@row.id">确认收货</button>
                        <a href="/MyOrder/OrderLogistics?order_id=@row.id" class="ui-btn ui-btn-border">查看物流</a>
                    }
                    @*<button class="ui-btn ui-btn-border" type="button">查看物流</button>*@
                </div>
            </li>
        }
    }

    <page rows="@Model.rows.Count()" total_result="@Model.total_result"></page>
}
<script type="text/javascript">
    $('.cancel').click(function () {
        var _this = this;
        swal({
            title: "您确定取消订单吗？",
            //text: "您确定取消订单吗？",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "",
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            closeOnConfirm: true
            //设置closeOnConfirm: false，下面二次确认按钮被点击后才会关闭模态框
        },
            function (isConfirm) {
                if (isConfirm) {
                    $.ajax({
                        url: '/MyOrder/CanelOrder', type: "post", data: { id: _this.value },
                        success: function (response) {
                            if (response.result_code == 1000) {
                                window.location.reload();
                            }
                            else {
                                layer.msg("你的订单取消失败");
                                //swal({ title: "取消！", text: "你的订单取消失败", type: "error" });
                            }

                        }
                    });
                } else {
                    layer.msg("你的订单取消失败");
                    //swal({ title: "取消！", text: "你的订单取消失败", type: "error" });
                }

            });
    });

    $('.pay').click(function () {
        var _this = this;
        var rqModel = {
            order_no: _this.value,
            wx_open_id: $("#wxOpenId").val()
        };
        swal({
            title: "您是否支付该订单吗？",
            //text: "您确定取消订单吗？",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "",
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            closeOnConfirm: true
            //设置closeOnConfirm: false，下面二次确认按钮被点击后才会关闭模态框
        },
            function (isConfirm) {
                if (isConfirm) {
                    $.ajax({
                        url: '/Order/WaitPay',
                        type: "post",
                        async: false,
                        data: rqModel,
                        success: function (response) {
                            if (response.result_code == 1000) {
                                if (isWxBrowser() == true) {//微信浏览器打开，公众号支付
                                    //layer.msg("请在微信外浏览器打开完成支付");
                                    //return false;
                                    var wcPayModel = $.parseJSON(response.result_msg);
                                    if (typeof WeixinJSBridge == "undefined") {
                                        if (document.addEventListener) {
                                            document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                                        } else if (document.attachEvent) {
                                            document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                                            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                                        }
                                    } else {
                                        onBridgeReady(wcPayModel);
                                    }
                                } else {
                                    location.href = response.result_msg;
                                }
                            } else {
                                layer.msg(response.result_msg);
                            }
                        }
                    });
                } else {
                    layer.msg("支付订单失败");
                }

            });
    });

    $('.receipt').click(function () {
        var _this = this;
        swal({
            title: "您确认收货吗？",
            //text: "您确定取消订单吗？",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "",
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            closeOnConfirm: true
            //设置closeOnConfirm: false，下面二次确认按钮被点击后才会关闭模态框
        },
            function (isConfirm) {
                if (isConfirm) {
                    $.ajax({
                        url: '/MyOrder/ConfirmOrder', type: "post", data: { id: _this.value },
                        success: function (response) {
                            if (response.result_code == 1000) {
                                window.location.reload();
                            }
                            else {
                                layer.msg("你确认收货失败");
                            }

                        }
                    });
                } else {
                    layer.msg("你确认收货失败");
                }

            });
    });

    $('.delete').click(function () {
        var _this = this;
        swal({
            title: "您确定删除订单吗？",
            //text: "您确定取消订单吗？",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "",
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            closeOnConfirm: true
            //设置closeOnConfirm: false，下面二次确认按钮被点击后才会关闭模态框
        },
            function (isConfirm) {
                if (isConfirm) {
                    $.ajax({
                        url: '/MyOrder/DeleteOrder', type: "post", data: { id: _this.value },
                        success: function (response) {
                            if (response.result_code == 1000) {
                                window.location.reload();
                            }
                            else {
                                layer.msg("你的订单删除失败");
                            }

                        }
                    });
                } else {
                    layer.msg("你的订单删除失败");
                }

            });
    });
</script>